package com.suzus.springbootrm.controller;

import com.suzus.springbootrm.config.ConfirmConfig;
import com.suzus.springbootrm.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        System.out.println("send "+message+" to ttl queue at "+new Date().toString());

        rabbitTemplate.convertAndSend("X","XA","come from 10s "+message);
        rabbitTemplate.convertAndSend("X","XB","come from 40s "+message);


    }

    @GetMapping("sendEMsg/{message}/{ttl}")
    public void sendEMsg(@PathVariable String message,@PathVariable String ttl){
        log.info("{},send {} to {}s queue",new Date().toString(),message,ttl);

        rabbitTemplate.convertAndSend("X","XC",message,msg->{
            msg.getMessageProperties().setExpiration(ttl);
            return msg;
        });
    }

    @GetMapping("sendPMsg/{message}/{ttl}")
    public void sendPMsg(@PathVariable String message,@PathVariable Integer ttl){
        log.info("{},send {} to {}ms queue",new Date().toString(),message,ttl);

        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,message, msg->{
            msg.getMessageProperties().setDelay(ttl);
            return msg;
        });
    }

    @GetMapping("sendCMsg/{message}")
    public void sendCMsg(@PathVariable String message){
        CorrelationData correlationData = new CorrelationData("1");

        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,ConfirmConfig.CONFIRM_ROUTING_KEY,message,correlationData);
        log.info("send {} to queue",message);
    }

}
